package admin;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class admin {
/*
    private int wno;
    private String wname;
    private int wage;
    private String wsex;
    private int whomeno;
    private String wjob;
    private String waddress;*/

    private int anum;

    private String apassword;

    Connection con=jdbcUtil.jdbcUtil.getConnection();


    public void setAnum(int anum) {
        this.anum = anum;
    }

    public void setApassword(String apassword) {
        this.apassword = apassword;
    }





    public boolean login(){
        PreparedStatement pstm=null;

        ResultSet rs=null;

        int sqlanum;
        String sqlapassword="";

        try {
            pstm=con.prepareStatement("SELECT * FROM admin WHERE anum=?");

            pstm.setInt(1,anum);

            rs=pstm.executeQuery();

            while (rs.next()){
                sqlapassword=rs.getString(2);
            }

        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("数据库炸了，少年，登录失败！");
            return false;
        }finally {
            jdbcUtil.jdbcUtil.close(null,pstm,rs);
        }
        if(sqlapassword.equals(apassword)){
            System.out.println("恭喜你登陆成功，少年");
            return true;
        }else{
            System.out.println("登录失败，同学");
            return false;
        }
    }


    public void listStudentInfo(){
        PreparedStatement pstm=null;

        ResultSet rs=null;

        try {
            pstm=con.prepareStatement("SELECT * FROM student");

            rs=pstm.executeQuery();
            while(rs.next()){
                System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+
                rs.getString(3)+" "+rs.getString(4)+" "+rs.getString(5)+" "+
                rs.getString(6)+" "+rs.getString(7));
            }

        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("数据库炸了，少年，查询失败！");
        }finally {
            jdbcUtil.jdbcUtil.close(null,pstm,rs);
        }

    }

    public void listWorkerInfo(){
        PreparedStatement pstm=null;

        ResultSet rs=null;

        try {
            pstm=con.prepareStatement("SELECT * FROM worker");

            rs=pstm.executeQuery();
            while(rs.next()){
                System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+
                        rs.getString(3)+" "+rs.getString(4)+" "+rs.getString(5)+
                        " "+rs.getString(6)+" "+rs.getString(7));
            }

        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("数据库炸了，少年，查询失败！");
        }finally {
            jdbcUtil.jdbcUtil.close(null,pstm,rs);
        }

    }

    public void listBuildingInfo(){
        PreparedStatement pstm=null;

        ResultSet rs=null;

        try {
            pstm=con.prepareStatement("SELECT * FROM building");

            rs=pstm.executeQuery();
            while(rs.next()){
                System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+
                        rs.getString(3)+" "+rs.getString(4));
            }

        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("数据库炸了，少年，查询失败！");
        }finally {
            jdbcUtil.jdbcUtil.close(null,pstm,rs);
        }

    }


    public void listDormitoryInfo(){
        PreparedStatement pstm=null;

        ResultSet rs=null;

        try {
            pstm=con.prepareStatement("SELECT * FROM dormitory");

            rs=pstm.executeQuery();

            while(rs.next()){
                System.out.println(rs.getString(1)+" "+rs.getString(2)+
                       " "+ rs.getString(3)+" "+rs.getString(4)+" "+rs.getString(5)+
                       " "+ rs.getString(6)+" "+rs.getString(7));
            }

        } catch (SQLException e) {
             e.printStackTrace();
            System.out.println("数据库炸了，少年，查询失败！");
        }finally {
            jdbcUtil.jdbcUtil.close(null,pstm,rs);
        }

    }
    public void listAdminInfo(){
        PreparedStatement pstm=null;

        ResultSet rs=null;
        System.out.println("只能查看自己的账号和密码");
        try {
            pstm=con.prepareStatement("SELECT * FROM admin WHERE anum=?");
            pstm.setInt(1,anum);

            rs=pstm.executeQuery();

            while(rs.next()){
                System.out.println(rs.getString(1)+" "+rs.getString(2));
            }

        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("数据库炸了，少年，查询失败！");
        }finally {
            jdbcUtil.jdbcUtil.close(null,pstm,rs);
        }
    }


    public void exit(){
        jdbcUtil.jdbcUtil.close(con,null,null);
    }


}
